package com.shiro;

import com.enumeration.JwtExceptionEnum;
import com.service.UserService;
import com.utils.JwtUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * CustomRealm.java
 *
 * @author Liang 649355204@qq.com
 * CreateTime 2019/7/15 14:50
 */
@Component
public class CustomRealm extends AuthorizingRealm {

    @Resource
    @Lazy
    UserService userService;

    /**
     * 必须重写此方法，不然会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

//    @Autowired
//    private void setUserMapper(XtUserMapper userMapper) {
//        this.userMapper = userMapper;
//    }

    /**
     * 获取身份验证信息
     * Shiro中，最终是通过 Realm 来获取应用程序中的用户、角色及权限信息的。
     *
     * @param authenticationToken 用户身份信息 token
     * @return 返回封装了用户信息的 AuthenticationInfo 实例
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("————身份认证方法————");
        String token = (String) authenticationToken.getCredentials();
        // 解密获得username，用于和数据库进行对比
        String uuid = JwtUtil.getUuid(token);
        System.out.println("*************" + uuid);
        if (!uuid.equals("")) {
            int verifyRtn = JwtUtil.verify(token, uuid);
            System.out.println(verifyRtn);
            if (verifyRtn == JwtExceptionEnum.JWT_MATCH.getCode()) {
                return new SimpleAuthenticationInfo(token, token, getName());
            } else if (verifyRtn == JwtExceptionEnum.JWT_NOT_MATCH.getCode()) {
                throw new AuthenticationException("token认证失败！");
            } else if (verifyRtn == JwtExceptionEnum.JWT_EXPIRE.getCode()) {
                throw new AuthenticationException("token_expired");
            }
            throw new AuthenticationException("token认证失败！");
        }
        throw new AuthenticationException("token认证失败！");
    }

    /**
     * 获取授权信息
     *
     * @param principals 主要内容
     * @return AuthorizationInfo
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("————权限认证————");
        String uuid = JwtUtil.getUuid(principals.toString());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获得该用户角色
        String role = userService.getRole(uuid);
        //每个角色拥有默认的权限
//        List<String> permissionList = userService.getPermissionList(uuid);
        //设置该用户拥有的角色和权限
        info.addRole(role);
        System.out.println(role);
//        info.addStringPermissions(permissionList);
        return info;
    }
}
